package com.bitrate.dify.wx.mp.handler;

import cn.hutool.core.text.UnicodeUtil;
import com.bitrate.dify.client.DifyClient;
import com.bitrate.dify.request.ChatCompletionRequest;
import com.bitrate.dify.response.ChatCompletionResponse;
import com.bitrate.dify.sse.SseEventSourceListener;
import com.bitrate.dify.util.JsonUtils;
import com.bitrate.dify.wx.mp.builder.TextBuilder;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.UnsupportedEncodingException;
import java.util.Map;

import static me.chanjar.weixin.common.api.WxConsts.XmlMsgType;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Slf4j
@Component
public class MsgHandler extends AbstractHandler {

    @Autowired
    private DifyClient difyClient;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Lazy
    @Autowired
    private WxMpService wxMpService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService weixinService,
                                    WxSessionManager sessionManager) {

        if (!wxMessage.getMsgType().equals(XmlMsgType.EVENT)) {
        }
        //暂时只能识别文字信息、
        String content = "";
        String fromUser = wxMessage.getFromUser();
        Object object = redisTemplate.opsForValue().get(fromUser);
        if (wxMessage.getMsgType().equals(XmlMsgType.TEXT)) {
            String conversationId = "";
            //获取上下文会话id
            if (object == null) {
                //通过BLOCK模式--->模拟获取会话id
                ChatCompletionRequest request = new ChatCompletionRequest();
                request.setUser(fromUser);
                request.setQuery("你好");
                request.setConversation_id("");
                ChatCompletionResponse response = difyClient.blockChatCompletions(request);
                conversationId = response.getConversation_id();
                redisTemplate.opsForValue().set(fromUser, conversationId);
                redisTemplate.opsForValue().set(conversationId, fromUser);
            }else{
                conversationId = object.toString();
            }
            ChatCompletionRequest request = new ChatCompletionRequest();
            request.setUser(fromUser);
            request.setQuery(wxMessage.getContent());
            request.setConversation_id(conversationId);
            SseEmitter sseEmitter = new SseEmitter();
            difyClient.streamChatCompletions(request, new SseEventSourceListener(sseEmitter, redisTemplate, wxMpService));
        }
        return new TextBuilder().build(content, wxMessage, weixinService);
    }

}
